package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.DraftLog;
import com.mbyte.easy.admin.service.IDraftLogService;
import com.mbyte.easy.common.constant.DraftLogConstant;
import com.mbyte.easy.common.constant.StatusPPTAudio;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.util.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Slf4j
@Controller
@RequestMapping("/admin/draftLog")
public class DraftLogController extends BaseController  {

    private String prefix = "admin/draftLog/";

    @Autowired
    private IDraftLogService draftLogService;
    @Autowired
    private IUserService userService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param draftLog
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String createTimeSpace, String updateTimeSpace, DraftLog draftLog,Long id) {
        Page<DraftLog> page = new Page<DraftLog>(pageNo, pageSize);
        QueryWrapper<DraftLog> queryWrapper = new QueryWrapper<DraftLog>();
        if(!ObjectUtils.isEmpty(draftLog.getUserId())) {
            queryWrapper = queryWrapper.like("user_id",draftLog.getUserId());
         }
        if(!ObjectUtils.isEmpty(draftLog.getActionContent())) {
            queryWrapper = queryWrapper.like("action_content",draftLog.getActionContent());
         }
        if(!ObjectUtils.isEmpty(draftLog.getActionType())) {
            queryWrapper = queryWrapper.like("action_type",draftLog.getActionType());
         }
        if(!ObjectUtils.isEmpty(draftLog.getPrimaryState())) {
            queryWrapper = queryWrapper.like("primary_state",draftLog.getPrimaryState());
         }
        if(!ObjectUtils.isEmpty(draftLog.getAfterState())) {
            queryWrapper = queryWrapper.like("after_state",draftLog.getAfterState());
         }
        if(!ObjectUtils.isEmpty(draftLog.getReason())) {
            queryWrapper = queryWrapper.like("reason",draftLog.getReason());
         }

        List<DraftLog> draftLogs = draftLogService.listByDraftId(id);
        if (draftLogs.size()!=0){
            queryWrapper.lambda().in(DraftLog::getId,draftLogs.stream().map(DraftLog::getId).collect(Collectors.toList()));
        }

        //名字
        List<DraftLog> list = draftLogService.list(queryWrapper);
        Map<Long,String> map=new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i).getUserId(),userService.selectByPrimaryKey(list.get(i).getUserId()).getUsername());
        }

        IPage<DraftLog> pageInfo = draftLogService.page(page, queryWrapper);
        model.addAttribute("searchInfo", draftLog);
        model.addAttribute("map", map);
//        model.addAttribute("status", );
        model.addAttribute("typeMap", DraftLogConstant.ACTION_MAP);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param draftLog
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(DraftLog draftLog){
        return toAjax(draftLogService.save(draftLog));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("draftLog",draftLogService.getById(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param draftLog
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(DraftLog draftLog){
        return toAjax(draftLogService.updateById(draftLog));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(draftLogService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(draftLogService.removeByIds(ids));
    }

    //操作记录
    @GetMapping("operation/{id}")
    public String operation(Model model,@PathVariable("id")Long id,String userName){

        log.info("==========username====="+userName);
        List<DraftLog> draftLogs = draftLogService.listByDraftId(id);
        log.info("==========list"+draftLogs);

        model.addAttribute("list",draftLogs);
        model.addAttribute("userName",userName);
        model.addAttribute("id",id);
        model.addAttribute("status", DraftLogConstant.ACTION_MAP);
        model.addAttribute("state", StatusPPTAudio.ACTION_MAP_STATUS);
        return prefix+"list";
    }
    //操作记录
    @PostMapping("searchOperation")
    public String searchOperation(Model model,Long id,String userName,String status){

        List<DraftLog> draftLogs = draftLogService.listByDraftId(id);

        if (!ObjectUtils.isEmpty(userName)){
            for (int i = 0; i < draftLogs.size(); i++) {
                if (!draftLogs.get(i).getUserName().contains(userName)){
                    draftLogs.remove(i);
                    i--;
                };
            }
        }
        if (!ObjectUtils.isEmpty(status)){
            for (int i = 0; i < draftLogs.size(); i++) {
                if (!draftLogs.get(i).getActionType().contains(status.trim())){
                    draftLogs.remove(i);
                    i--;
                };
            }
        }

        model.addAttribute("list",draftLogs);
        model.addAttribute("userName",userName);
        model.addAttribute("id",id);
        model.addAttribute("status", DraftLogConstant.ACTION_MAP);
        model.addAttribute("choose", status);
        model.addAttribute("state", StatusPPTAudio.ACTION_MAP_STATUS);
        return prefix+"list";
    }


}

